package com.anis.persistance.save;

import java.util.ArrayList;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import com.anis.jpa.project.entities.Helper;
import com.anis.jpa.project.entities.Patient;
import com.anis.jpa.project.entities.RadioReport;


public class SaveData {

	private EntityManagerFactory emf = Persistence
			.createEntityManagerFactory("JPAService");
	private EntityManager em = emf.createEntityManager();

	public SaveData() {
		super();
		// TODO Auto-generated constructor stub
	}

	public void savePatient(String name, String lastName, String address,
			String idPatient, int phone, ArrayList<RadioReport> radioReports)
			throws Exception {
		em.getTransaction().begin();
		Patient patient = new Patient();
		patient.setIdPatient(idPatient);
		patient.setPhone(phone);
		patient.setPatientAddress(address);
		patient.setPatientLastName(lastName);
		patient.setRadioReports(radioReports);
		em.persist(patient);
		em.flush();
		Patient st = em.find(Patient.class, patient.getIdPatient());
		System.out.println(st);

		em.getTransaction().commit();

		em.close();
		emf.close();

		Helper.checkData();
	}

}
